package com.taiyuan.zfmvp.JUC.three;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption: 循环栅栏
 * @Date 2022/10/6
 * @return
 */
public class CyclicBarrierDemo {

    private static  final  int number = 8;
    //集齐七龙珠召唤神龙
    public static void main(String[] args) {

        //创建Cyclicbarrier
        CyclicBarrier cyclicBarrier = new CyclicBarrier(number, new Runnable() {
            @Override
            public void run() {
                System.out.println("集齐七龙珠召唤神龙");
            }
        });

        //集齐七颗龙珠过程
        for (int i = 0; i < 7; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //等待
                    try {
                        System.out.println(Thread.currentThread().getName()+ " 星龙珠被收集");
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            },String.valueOf(i)).start();
        }

    }

}
